/**
 * Created by Administrator on 2017/6/25/025.
 */
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

const moduleA = {
  state: {
    name: 'A'
  }
}
const moduleB = {
  state: {
    name: 'B'
  }
}
const store = new Vuex.Store({
  state: {
    count: 0,
    msg: 'Hello Vuex'
  },
  mutations:{
  // 同步
    add(state){
      state.count++;
    },
    addStr(state,str){
      state.msg += str;
    },
    reduce(state){
      state.count--;
    }
  },
  actions: {
  // 通过actions异步调用mutations中的方法
    addStrAction(context,str) { // context: 上下文对象，可理解为store对象
      context.commit('addStr',str)
    },
    addAction({commit}) { // { commit }: commit对象，简化代码
      commit('add')
    },
    reduceAction({commit}) {
      commit('reduce')
    },
    printAction(context) {
      console.log(context.state.msg)
    }
  },
  modules:{
    a: moduleA ,
    b: moduleB
  }
})
export default store
